﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Collections;
using System.Xml.Serialization;

namespace ВУИ
{
    public partial class fLogin : Form
    {
        Form1 Main;
        public fLogin(Form1 main)
        {
            InitializeComponent();
            Main = main;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            
            

        }

        private void radioButton1_CheckedChanged_1(object sender, EventArgs e)
        {
            panel1.Visible = radioButton1.Checked;
            label5.Visible = !radioButton1.Checked;
        }

        private void fLogin_Load(object sender, EventArgs e)
        {
              
        }

        private void button1_Click(object sender, EventArgs e)
        {
            log l = new log();
            l.user = textBox1.Text;
            l.pass = maskedTextBox1.Text;
            l.type = comboBox1.SelectedIndex == 0;
            l.adress = textBox2.Text;
              XmlSerializer xml;
            FileStream stream;


            if (File.Exists("autosave.xml"))
            {
                xml = new XmlSerializer(typeof(autosave));
                stream = new FileStream("autosave.xml", FileMode.Open, FileAccess.Read);
                autosave a = (autosave)xml.Deserialize(stream);
                stream.Close();
                if ((a.log.adress == l.adress) & (a.log.db == l.db) & (a.log.user == l.user) & (a.log.pass == l.pass) & (a.log.type == l.type))
                {
                    DialogResult result;
                    result = MessageBox.Show("В последний раз вы работали с это БД и не сохранили по какойто причине внесенные изменения. Существует резервная копия БД сохраненна " + a.date.ToShortDateString() + " в " + a.date.ToShortTimeString() + ". Востановить эту версию БД?", "Востановление данных.", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {

                        Main.loginingauto(l);
                    }

                    else
                    {
                        File.Delete("autosave.xml");
                        if (Directory.Exists("temp"))
                        foreach (string s in Directory.GetFiles("temp"))
                            File.Delete( s);
                        Directory.Delete("temp");
                        Main.logining(l);

                    }


                }
                else Main.logining(l);
            }
            else
            Main.logining(l);
        }

        private void fLogin_FormClosed(object sender, FormClosedEventArgs e)
        {
           
        }
    }
}
